package org.news.controller;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Assert;
import org.junit.internal.Classes;
import org.news.beans.Users;
import org.news.dao.repository.UsersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user/")
public class UsersCtroller {
    @Autowired
    private UsersRepository usersRepository;
    @RequestMapping( value = "/login")
    public String login(Model model){
        model.addAttribute("user",new Users());
        return "login";
    }
    @RequestMapping( value = "/logined")
    public String dologin(Model model, Users users, HttpServletRequest request, Users user, HttpServletResponse response) {
        if (usersRepository.findOneObject(users) != null) {
            model.addAttribute("user", usersRepository.findOneObject(users));
            Users user1 = usersRepository.findOneObject(users);
            Cookie cookie = new Cookie("user", user1.getUsername());
            cookie.setPath("/");
            String ip = request.getRemoteAddr();
            Cookie cookieip = new Cookie("ip", ip);
            cookieip.setPath("/");
            String time = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
            Cookie cookietime = new Cookie("time", time);
            cookietime.setPath("/");
            String lala =  user1.getUserid()+"";
            Cookie cookieinfo = new Cookie("userid", lala);
            cookieinfo.setPath("/");
            int has = Integer.parseInt(request.getParameter("jl"));
            if (has > 0) {
                cookieip.setMaxAge( 60 * 60 * 24 * 30);
                cookietime.setMaxAge( 60 * 60 * 24 * 30);
                cookie.setMaxAge( 60 * 60 * 24 * 30 );
                cookieinfo.setMaxAge( 60 * 60 * 24 * 30 );
            }
            response.addCookie(cookieinfo);
            response.addCookie(cookie);
            response.addCookie(cookieip);
            response.addCookie(cookietime);
            return "cookie";
        } else {
            request.setAttribute("error", "ok");
            return login( model);
        }
    }
    @RequestMapping( value = "/include")
    private String pageTo(HttpServletRequest request, HttpServletResponse response){
        String op = request.getParameter("include");
        Cookie[] cookies = request.getCookies();
        System.out.println(cookies[1].getName()+"--->"+cookies[1].getValue());
        return "Manage.jsp?include=" + op;
    }
    @RequestMapping("/reset")
    private String userReset(HttpServletRequest request, HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        Users user = new Users(Integer.parseInt(cookies[1].getValue()),cookies[2].getValue(),null);
        String newpwd = request.getParameter("newpwd");
        String oldpwd = request.getParameter("oldpwd");
        user.setUserpwd(oldpwd);
        System.out.print(newpwd);
        if( usersRepository.findOneObject(user) != null ){
            user.setUserpwd(newpwd);
            usersRepository.update(user);
            return "login";
        } else {
            request.setAttribute("error", "对不起原密码不正确");
            return "Manage.jsp?include=reset";
        }
    }
    @RequestMapping("/logout")
    private String userLogout(HttpServletRequest request, HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        for(int i = 0,len = cookies.length; i < len; i++) {
            Cookie cookie = new Cookie(cookies[i].getName(),null);
            cookie.setMaxAge(0);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        return "cookie";
    }
//    @RequestMapping("/yy")
//    public String GetAllUser(){
//        System.out.println(usersRepository);
//        List<Users> usersList = usersRepository.findAll();
//        for (Users u : usersList){
//            System.out.print(u.getUsername());
//        }
//        return "index";
//    }
//    @RequestMapping("/register")
//    public String usersRegister( Model model){
//        model.addAttribute("users",new Users());
//        return "register";
//    }
    @RequestMapping("/nn")
    public String GetUser(){
        System.out.println(usersRepository);
        String name = "ass";
        Users users = usersRepository.findOneObject(new Users());
        return "index";
    }

}




















